from online_shop.cart.models import CartItem
from online_shop.products.models import products
from django.shortcuts import get_object_or_404 
from django.http import HttpResponseRedirect
import random

CART_ID_SESSION_KEY = 'cart_id'

def _cart_id(request):
    if request.session.get(CART_ID_SESSION_KEY,'') == '':
        request.session[CART_ID_SESSION_KEY] = _generate_cart_id()
    return request.session[CART_ID_SESSION_KEY]
    
def _generate_cart_id():
    cart_id = ''
    characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890!@#$%^&*()'
    cart_id_length = 50
    for y in range(cart_id_length):
        cart_id += characters[random.randint(0, len(characters)-1)]
    return cart_id

#return all items from the current user's cart
def get_cart_items(request):
    return CartItem.objects.filter(cart_id=_cart_id(request))
    
# add an item to the cart
def add_to_cart(request):
    postdata = request.POST.copy()
    # get product id from post data, return 0 if empty
    product_id = postdata.get('product-id',0)
    # get quantity added, return 1 if empty
    quantity = postdata.get('buy-amount',1)
    # fetch the product or return a missing page error
    p = get_object_or_404(products, id=product_id)
    # get products in cart
    cart_products = get_cart_items(request)
    product_in_cart = False
    # check to see if item is already in cart
    for cart_item in cart_products:
        if cart_item.product.id == p.id:
            # update the quantity if found
            cart_item.augment_quantity(quantity)
            product_in_cart = True
    if not product_in_cart:
        # create and save a new cart item
        ci = CartItem()
        ci.product = p
        ci.quantity = quantity
        ci.cart_id = _cart_id(request)
        ci.save()

# returns the total number of items in the user's cart
def cart_distinct_item_count(request):
    cart_items = get_cart_items(request)
    items_amount = 0
    for cart_item in cart_items:
        items_amount += cart_item.quantity
    return items_amount

def get_single_item(request, item_id): 
    return get_object_or_404(CartItem, id=item_id, cart_id=_cart_id(request))
    
# update quantity for single item
def update_cart(request):
    """ function takes a POST request that updates the quantity for single product instance in the 
    current customer's shopping cart 
    
    """
    postdata = request.POST.copy()
    item_id = postdata['item_id']
    quantity = postdata['quantity']
    cart_item = get_single_item(request, item_id)
    if cart_item:
        if int(quantity) > 0:
            cart_item.quantity = int(quantity)
            cart_item.save()
            return cart_item
        else:
            remove_from_cart(request)
    
# remove a single item from cart
def remove_from_cart(request):
    """ function that takes a POST request removes a single product instance from the current customer's
    shopping cart
    """
    postdata = request.POST.copy()
    item_id = postdata['item_id']
    cart_item = get_single_item(request, item_id)
    if cart_item:
        cart_item.delete()
    
def cart_subtotal(request):
    """ gets the subtotal for the current shopping cart """
    cart_total = 0
    cart_products = get_cart_items(request)
    for cart_item in cart_products:
        cart_total += cart_item.product.price * cart_item.quantity
    return cart_total